package me.yobol.greentable.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.yobol.greentable.bean.CartCommodityBean;
import me.yobol.greentable.service.CartCommodityService;

/**
 * @author Yobol
 *
 */
public class GetCartCommodityListServlet extends HttpServlet {

	private static final long serialVersionUID = 8187033957689801303L;

	public GetCartCommodityListServlet() {
		super();
	}
	
	public void init() throws ServletException {
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int customerId = (request.getSession().getAttribute("uId") == null) ? -1
				: (Integer) request.getSession().getAttribute("uId");

		String redirect = null;
		
		if(customerId > 0) {
			List<CartCommodityBean> ccByCustomerId = CartCommodityService.getCartCommodityByCustomerId(customerId);
			request.getSession().setAttribute("cart", ccByCustomerId);
			redirect = "/pages/cart.jsp";
		}
		
		response.sendRedirect(request.getContextPath() + redirect);
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

}
